const { body } = require("express-validator");
const { User } = require("../../model/index");
const {validate} = require("./errBack");

// 这里是登录的注释
const login = validate([
  body("email")
    .notEmpty()
    .withMessage("邮箱不能为空")
    .isEmail()
    .withMessage("请输入正确邮箱格式")
    .custom(async (val) => {
      const dbBack = await User.findOne({ email: val });
      if (!dbBack) {
        return Promise.reject("邮箱未注册");
      }
    }),
]);

/* 这里是注册的注释 */
const register = validate([
  body('email').notEmpty()
  .withMessage("邮箱不能为空")
  .isEmail()
  .withMessage("请输入正确邮箱格式").custom(async (val) => {
    const dbBack = await User.findOne({ email: val });
    if (dbBack) {
      return Promise.reject("邮箱已注册");
    }
  }),
])

module.exports = {
  login,
  register
};
